home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / comm / mail / TASC.lha / tasc / rexx / TASC.thor < prev   
Encoding:
Text File  |  1997-06-27  |  10.7 KB  |  458 lines

  1. /* $VER: TASC.thor 1.6 (26.06.97)
  2.  *
  3.  *
  4.  * Concept & Original Scripts by: Kirk Strauser <kstrauser@gxl.com>
  5.  * and Adrian Knight <ajk@dial.pipex.com>
  6.  *   
  7.  * Rewritten by: Andy Macklin <andy@toadhall.u-net.com>
  8.  */
  9.  
  10.  
  11. /* Read the config file & set up defalts if it isn't there */
  12. if open(cfg,'Env:thor/TASC.cfg',r) then do
  13.    do until eof(cfg)
  14.        lin=readln(cfg)
  15.        parse var lin id ':' V
  16.        V=strip(V)
  17.        if upper(left(id,6))='DELMSG' then
  18.           delmsg=V
  19.        if upper(left(id,3))='URG' then
  20.           urg=V
  21.        if upper(left(id,6))='MAILHD' then
  22.           mailhd=V
  23.        if upper(left(id,6))='NEWSHD' then
  24.           newshd=V
  25.        if upper(left(id,6))='GLOBPM' then
  26.           GlobPM=V
  27.        if upper(left(id,10))='POSTMASTER' then
  28.           Postmaster=V
  29.        if upper(left(id,8))='DATABASE' then
  30.           spamdb=V
  31.     end
  32.     call close(cfg)
  33. end
  34. else do
  35.     delmsg='N'
  36.     urg='N'
  37.     mailhd='Email spam'
  38.     newshd='Usenet spam/mail fraud'
  39.     globPM='N'
  40.     Postmaster=''
  41.     spamdb='rexx/spam.db'
  42. end
  43.  
  44. /*=======================================================*/
  45. /* You're not supposed to change anything from here down */
  46. /*=======================================================*/
  47.  
  48. Parse ARG CLIARG
  49. CLIARG=upper(CLIARG)
  50. if CLIARG~='AUTO' & CLIARG~='' then do
  51.     say 'Template: Spam-O-Matic.thor AUTO/S'
  52.     say 'Run this script from within Thor'
  53.     exit
  54. end
  55.  
  56. options results
  57. options failat 31
  58.  
  59. CDB_MAIL                = 1   /* Private mail conference. */
  60.  
  61. thorport = address()
  62. if left(thorport, 5) ~= 'THOR.' then do
  63.   say 'Cannot find thorport.'
  64.   exit
  65. end
  66.  
  67. if ~show('p', 'BBSREAD') then do
  68.   address command
  69.   "run >nil: `GetEnv THOR/THORPath`bin/LoadBBSRead"
  70.   "WaitForPort BBSREAD"
  71. end
  72.  
  73. if open(A,'env:thor/thorpath') then do
  74.     THORP=READLN(A)
  75.     call close(A)
  76. end
  77.  
  78. address(thorport)
  79.  
  80. CURRENTMSG stem MSG
  81. if (rc ~= 0) then call oops("No current message.")
  82.  
  83. SAVEMESSAGE CURRENT FILE "T:tasc.tempfile"
  84. if(rc ~= 0) then call oops("Can't save current message.")
  85.  
  86. IF Open(A,'T:tasc.tempfile','r') = 0 THEN
  87.         call oops("Couldn't open temporary file.")
  88.  
  89.  
  90. i='1'
  91. flame.=''
  92. toaddr.=''
  93. do until hder=''
  94.     hder= readln(A)
  95.     if upper(left(hder,8)) = 'RECEIVED' then do        
  96.         Call Recd
  97.     end
  98.     if upper(left(hder,7)) = 'MESSAGE' then do
  99.         Call Mess
  100.     end
  101.     if upper(left(hder,6)) = 'RETURN' then do
  102.         Call Rtn
  103.     end
  104.     if upper(left(hder,5)) = 'REPLY' then do
  105.         Call Rply
  106.     end
  107.     if upper(left(hder,5)) = 'FROM:' then do
  108.         Call Frm
  109.     end
  110.     if upper(left(hder,5)) = 'PATH:' then do
  111.         Call Pth
  112.     end
  113.     if upper(left(hder,4)) = 'NNTP' then do
  114.         Call nntp
  115.     end
  116. end
  117. call close(A)
  118. /* Set the values for the Mailserver */
  119. Call Radd
  120. /* Parse out the next level of the internet heirachy to complain to */
  121. Call Boss
  122. /* remove unneeded addresses (if appropriate)*/
  123. if CLIARG~='AUTO' then do
  124.     Call Update
  125. end
  126. /* Check for known bad ISPs, undeliverable addresses or specific addresses for abuse. */
  127. Call Undeliverable
  128.  
  129. address BBSREAD
  130.  
  131. READBRMESSAGE bbsname '"'MSG.BBSNAME'"' confname '"'MSG.CONFNAME'"' msgnr MSG.MSGNR headstem HEADTAGS textstem TEXTTAGS 
  132. if (rc ~= 0) then call oops
  133.  
  134. getconfdata bbsname '"'MSG.BBSNAME'"' confname '"'MSG.CONFNAME'"' stem CONFDATA
  135. if (rc ~= 0) then call oops
  136.  
  137. UNIQUEMSGFILE bbsname '"'MSG.BBSNAME'"' stem tmp
  138. if (rc ~= 0) then call oops
  139.  
  140. /* Build the outgoing message */
  141.  
  142. if Postmaster~='' then do
  143.     address command
  144.     'echo "From: postmaster@'||Postmaster||'" > t:tasc.tempH'
  145.     'Echo "" >> t:tasc.tempH'
  146. end
  147. if ~bittst(CONFDATA.FLAGS,CDB_MAIL) then do
  148.     address command 'Type "'||THORP||'rexx/SpamNewsHeader" >> T:tasc.tempH'
  149.     end
  150. else do
  151.     address command 'Type "'||THORP||'rexx/SpamMailHeader" >> T:tasc.tempH'
  152. end
  153. address command 'Join t:tasc.tempH t:tasc.tempfile as 'tmp.NAME
  154. if (rc ~= 0) then call oops("Unable to build message file.")
  155.  
  156. /* Choose the addresses from the header & the hotlist to send the complaint to */
  157. if CLIARG~='AUTO' then do
  158.     Call Chooser
  159. end
  160. else do
  161.     Call AutoR
  162. end
  163.  
  164. /* Use the appropriate mailer to send it */
  165. call WriteThorMessage
  166.  
  167. if (rc ~= 0) then call oops
  168.  
  169. if delmsg='Y' then do
  170.     address(bbsread)
  171.     UPDATEBRMESSAGE '"'MSG.BBSNAME'"' '"'MSG.CONFNAME'"' msgnr MSG.MSGNR SETDELETED
  172. end
  173.  
  174. call tidy
  175.  
  176. Recd:
  177. parse VAR hder gubbins 'from ' addss ' by' remains
  178. /* addss should contain the details that we want */
  179. if index(addss,'[')~=0 then do
  180.    parse VAR addss '[' netnum ']' remains
  181.    /* extract the IP number, in case that's all there is*/
  182. end
  183. if index(addss,'(')~=0 then do
  184.    /* Search for brackets containing the details we need */
  185.    parse VAR addss rnme '(' netnum ')' remains
  186.    if index(rnme,'.')=0 then do /* pretty unlikely to be a mailable address */
  187.       if index(netnum,'[')~=0 then do
  188.         parse VAR netnum rnme '[' remains
  189.       end
  190.    end
  191.    if index(netnum,'[')~=0 then do
  192.       /* Parse netnum, one way or the other */      
  193.       parse VAR netnum '[' netnum ']' remains
  194.    end
  195.    if index(netnum,'(')~=0 then do
  196.         parse VAR netnum '(' netnum ')' remains
  197.    end
  198. end
  199. if index(netnum,'[')~=0 then do
  200.     parse VAR netnum '[' netnum ']'
  201. end
  202. netnum = '['||strip(netnum)||']'
  203. return
  204.  
  205. Mess:
  206. /* Message ID may contain a valid _Real_ domain to complain to */
  207. parse VAR hder gubbins '@' mnme '>'
  208. flame.i='Msg-ID:'||strip(mnme)
  209. i=i+1
  210. return
  211.  
  212. Rtn:
  213. /* Return Path might contain a valid _Real_ domain to complain to */
  214. parse VAR hder gubbins '@' rtnme '>'
  215. flame.i='Return Path:'||strip(rtnme)
  216. i=i+1
  217. return
  218.  
  219. Rply:
  220. /* Reply-to: might contain a valid _Real_ domain to complain to */
  221. parse VAR hder gubbins '@' rpnme ' ' remains
  222. flame.i='Reply-To:'||strip(rpnme)
  223. i=i+1
  224. return
  225.  
  226. Frm:
  227. /* From: might contain a valid _Real_ domain to complain to. I wish :( */
  228. parse VAR hder gubbins '@' fnme '>'
  229. flame.i='From:'||strip(fnme)
  230. i=i+1
  231. return
  232.  
  233. Pth:
  234. /* Search the path: header for possible addresses (news spam only) */
  235. P1=lastpos('!',hder)
  236. P2=lastpos('!',hder,P1-1)
  237. P1=substr(hder,P2+1)
  238. parse VAR P1 parth '!' gubbins
  239. flame.i='Path:'||strip(parth)
  240. i=i+1
  241. return
  242.  
  243. nntp:
  244. /* Just in case there is an nntp-posting-host header in the news spam */
  245. parse VAR hder gubbins ': ' nntpnme
  246. flame.i='NNTP-Host:'||strip(nntpnme)
  247. i=i+1
  248. return
  249.  
  250. Radd:
  251. if rnme~='RNME' then do
  252.     flame.i='Mailserver:'||strip(rnme)
  253.     i=i+1
  254. end
  255. if netnum~='NETNUM' then do
  256.     flame.i='Mailserver:'||netnum
  257.     i=i+1
  258. end
  259. return
  260.  
  261. AutoR:
  262. j=1
  263. toaddr.count=0
  264. do m=1 to (i-1)
  265.     parse VAR flame.m hder ':' tnme
  266.     if bittst(confdata.flags,CDB_Mail) then do
  267.         /* A Mail message */
  268.         if hder='Msg-ID' then do
  269.             call autoadd
  270.             end
  271.         if hder='Mailserver' then do
  272.             call autoadd
  273.             end
  274.         end
  275.     else do
  276.         if hder='NNTP-Host' then do
  277.             call autoadd
  278.             end
  279.         if hder='Path' then do
  280.             call autoadd
  281.             end
  282.     end
  283. end
  284. if toaddr.count=0 then do
  285.     address (thorport)
  286.     requestnotify text '"No addresses suitable for autoreply option"' BT '"_OK"'
  287.     call tidy
  288. end
  289. else do
  290.     drop flame.
  291.     flame.count=toaddr.count
  292.     do i=1 to toaddr.count
  293.         flame.i=toaddr.i
  294.     end
  295. end
  296. return
  297.  
  298.  
  299. autoadd:
  300. toaddr.j='auto:'||tnme
  301. j=j+1
  302. toaddr.count=toaddr.count+1
  303. return
  304.  
  305. Boss:
  306. /* Add the option to complain further up the internet hierachy */
  307. k=i-1
  308. do j=1 to k
  309.     parse VAR flame.j gubbins ':' lwr '.' hghr
  310.     if left(lwr,1)~='[' then do     /*Not an IP number */
  311.         if index(hghr,'.')~=0 then do
  312.             flame.i='Parent of '||gubbins||':'||hghr
  313.             i=i+1
  314.         end
  315.     end
  316. end
  317. return
  318.  
  319. Update:
  320. m=1
  321. toaddr.count=0
  322. do j=1 to (i-1)
  323.     parse VAR flame.j gubbins ':' jtmp
  324.     uniq=1
  325.     do k=(j+1) to i
  326.         parse VAR flame.k gubbins ':' ktmp
  327.         if jtmp = ktmp then do
  328.             uniq=0
  329.         end
  330.         if jtmp = '' then do
  331.             uniq=0
  332.         end
  333.     end
  334.     if uniq=1 then do
  335.         toaddr.m=flame.j
  336.         toaddr.count=toaddr.count+1
  337.         m=m+1
  338.     end
  339. end
  340. return
  341.  
  342. Undeliverable:
  343. if open(db,THORP||spamdb,r) then do
  344.     a=0
  345.     wrong.=''
  346.     correct.=''
  347.     do until eof(db)
  348.         lin=readln(db)
  349.         a=a+1
  350.         parse VAR lin wrong.a '->' correct.a
  351.     end
  352.     wrong.count=a
  353.     do m=1 to toaddr.count
  354.         parse VAR toaddr.m nme ':' oldaddr
  355.         do a=1 to wrong.count
  356.             if upper(oldaddr)=upper(wrong.a) then do
  357.                 if upper(correct.a)~='UNDELIVERABLE' then do
  358.                     if index(correct.a,'@')~=0 then do
  359.                         toaddr.m='+'||nme||'(Redirected):'||correct.a
  360.                     end
  361.                     else do
  362.                         toaddr.m=nme||'(Redirected):'||correct.a
  363.                     end
  364.                 end
  365.                 else do
  366.                     toaddr.m='-'||nme||':('||oldaddr||')'||correct.a
  367.                 end
  368.             end
  369.         end
  370.     end
  371. end
  372. call close(db)
  373. return
  374.  
  375. WriteThorMessage:
  376.    address BBSREAD
  377.    EVE_ENTERMSG = 0
  378.    drop EVENT.
  379.    EVENT.TONAME = ''
  380.    Do j=1 to flame.count
  381.        if globPM~='Y' then do
  382.          parse VAR flame.j tnme ':' tadd
  383.          EVENT.TONAME = EVENT.TONAME  || strip(tnme,B,'+') || ','
  384.          end
  385.        else do
  386.          EVENT.TONAME = EVENT.TONAME  || 'Postmaster,'
  387.          end
  388.    end
  389.    EVENT.TONAME = strip(EVENT.TONAME,T,',')
  390.    EVENT.TOADDR = ''
  391.    Do j=1 to flame.count
  392.        parse VAR flame.j tnme ':' tadd
  393.        if left(tnme,1)='+' then
  394.           EVENT.TOADDR = EVENT.TOADDR || tadd|| ','
  395.        else do
  396.           if left(tnme,1)='-' then
  397.             NOP
  398.           else
  399.             EVENT.TOADDR = EVENT.TOADDR || 'postmaster@'|| tadd ||','
  400.        end
  401.    end
  402.    EVENT.TOADDR=strip(EVENT.TOADDR,T,',')
  403.    if ~bittst(CONFDATA.FLAGS,CDB_MAIL) then do
  404.         EVENT.SUBJECT = newshd
  405.         end
  406.    else do
  407.         EVENT.SUBJECT = mailhd
  408.    end
  409.    EVENT.CONFERENCE = 'EMail'
  410.    EVENT.MSGFILE = tmp.FILEPART
  411.    if urg='Y' then do
  412.         EVENT.URGENT = 1
  413.         end
  414.    else do
  415.         EVENT.URGENT=0
  416.    end
  417.    WRITEBREVENT bbsname '"'MSG.BBSNAME'"' event EVE_ENTERMSG stem EVENT
  418. return
  419.  
  420. Chooser:
  421. /* Here's where the additional recipients like the IRS get added to the options */
  422. DROP flame.
  423. if open(A,THORP||'rexx/spamaddr','r') then do
  424.     DO WHILE ~Eof(A)
  425.         spama=readln(A)
  426.         if left(spama,1)='+' then do
  427.             i=toaddr.count+1
  428.             toaddr.i=spama
  429.             toaddr.count=toaddr.count+1
  430.         end
  431.     END
  432. end
  433. address(Thorport)
  434. Requestlist Instem toaddr outstem flame dragselect multiselect title '"Complain to"'
  435. IF (RC > 0) THEN DO
  436.    REQUESTNOTIFY TEXT '"No Addresses Selected"' BT '"_Ok"'
  437.    address command 'delete >nil: '||tmp.NAME
  438.    call tidy
  439.    EXIT
  440. END
  441. return
  442.  
  443. oops:
  444.   PARSE ARG errmsg
  445.   if errmsg = '' then do
  446.     if address() = "BBSREAD" then errmsg=BBSREAD.LASTERROR
  447.     else errmsg=THOR.LASTERROR
  448.   end
  449.   address(thorport)
  450.   REQUESTNOTIFY TEXT '"' errmsg '"' BT '"_Abort"'
  451.   call tidy
  452. return
  453.  
  454.  
  455. tidy:
  456.   address command 'delete >nil: T:tasc.temp#?'
  457. exit
  458.